Closed MacBook Pro keeps turning external display on/off [duplicate]
Sounds like you already have the suspects identified (power supply being intermittent, faulty port, faulty dongle (if in use), faulty HDMI lead). If I were you, I'd start a process of elimination. Source another power supply (and/or extension cord, if in use), swap out the dongle/port/HDMI lead. Also, try a different refresh rate/resolution for the display and see if that makes as difference. I'm using an early 2015 MacBook Pro with an Acer UHD4K2K display via display port and the laptop closed. I've had problems with the display not waking at times.
Also, check your Magsafe port for debris. Could be dirty contacts making the charging intermittent.