Do you only wish to have one device (HDMI/onboard) to output sound?
I had issues previously (on 16.04, different computer) and it was due to the power state of the monitor coming up as a different HDMI, so sometimes the sound went off because it was outputting to the wrong output.
I'd suggest installing pavucontrol
(PulseAudio Volume Control), since you can disable the one you don't use (onboard in my case)
If in the case of HDMI, select the correct output.
pavucontrol
can be seen as a more detailed version of the built-in Sound preferences in Control Center.