Same issue for me on my iMac (Retina 5K, 27-inch, 2020). Turns off unexpectedly, but only when I'm using an external monitor.
I took it in to the Apple Store and they wiped the drive and reinstalled the OS said everything should be fine. The issue restarted same day I brought it home. This is very frustrating.