While I am personally a huge proponent of taking an evolutionary perspective regarding biological phenomena (of which dreams are obviously one) I suspect we also need to be cautious on occasion. There’s always the temptation to create post-hoc explanations that while seemingly plausible may in fact be erroneous. For example, one could hypothesize that nightmares are an accidental side-effect of having a complex brain that has a wide variety of different mechanisms and which, every night, does a sort of “garbage collection” to reduce demands on memory storage and other aspects of cognition. Just because we suffer from bad backs when we grow older because the human spine was originally evolved for quadripedal movement and today must do duty in bipedal motion, we ought not create a story that explains how bad backs are evolutionarily advantageous.
For me, the jury is very much still out on the question of nightmares. It would be instructive to design an experiment that could determine which of the two hypotheses (“evolved mechanism” or “accidental side-effect”) are closer to the truth.