This can be an issue with the notebook power supply. Power drain the computer once and then check if the same issue still exist. To power drain the computer, disconnect all the external devices from the computer and also take out the charger and battery, then press and hold the power button for 30 sec and then release it.
Also do a BIOS update for your notebook if available on HP.com for model.
You can also check the computer in BIOS. Power on the computer and immediately tap the F10 key to load the BIOS, then leave the computer as it is in the BIOS screen for some time and check if the computer still turns off and if the notebook still turns off on its own while it is in BIOS then contact HP and arrange for the service on the computer