You might want to try booting into Safe Mode and leave it that way for a while to see if it spontaneously restarts again. This Apple Support article describes how to use Safe Mode: Start up your Mac in safe mode - Apple Support
I'd also ask if you have any anti-virus, "app cleaners", "Mac optimizers" or VPN software installed? These have all been know to create havoc on Macs, and actually create more problems than they prevent. If you do, I strongly urge you to delete them, using the manufacturer's uninstaller, as simply dragging them to the Trash often times doesn't delete everything.
Hope this helps.