Dr Mariano puts this down to norepinephrine, like Peak said (he called it noradrenaline, same thing). It's basically low enough in the middle of the night to allow for erections, but when you wake up (and NE is largely responsible for waking you up) it's too high. NE is actually very important for erections and especially ejaculation, when NE shoots up and dopamine goes down momentarily.
FWIW, the single biggest influence on my getting better erections has been starting levothyroxine. But getting E2 levels out of line can undo this, as E2 influences thyroid through increasing thyroid binding globulin, thereby reducing free thyroid hormones (T3 and T4). E2 also increases norepinephrine in its own way, as well as ACTH and cortisol (assuming your cortisol is functioning well, of course). So with E2 being too high you get the "double whammy" of lowering free thyroid hormone as well as causing some degree of sympathetic nervous system activation with the increase in norepinephrine, ACTH, and cortisol.
The biggest contributor to norepinephrine problems, again citing Mariano, is hypothyroidism. But low cortisol is also a less likely possibility, seeing how cortisol helps balance norepinephrine such that too-low cortisol (in view of stress) means higher NE.