Reading around freuds interpretation of dreams, one idea is that that the dream is a production to satisfy drives and repressed urges to allow you to keep sleeping. So the dream lets me fuck my teacher so I don't wake up to try to actually fuck my teacher or have a wank when that drive arises. Rest is important.
There's been recent research on learning and memory consolidation in dreams, that makes sense to me. I definitely feel a sense of replenishment and tidying when I sleep, I often go to bed with pessimism and problems and wake up with optimism and solutions that seem simple, but it's impossible to say how much of that's down to rest and how much is some therapeutic cognitive theatre.
In terms of analysing dreams, I'm sceptical of all that because I think what's recalled is often extremely precarious and linked to lots of chaotic factors like the emotional impact, when you wake up and how easy the dream is to communicate... at best you get an essence, and even the rare times you remember in detail, you can feel the loss occur as you put it into words.
So maybe it's stupid to think there's an answer to why we dream, we aren't so teleological about other forms of consciousness, maybe that's because dreaming is when we're least in control, so we want to know who's in charge.