Macbook runs consistently hot sometimes, but consistently cool other times
It is not normal to have computer overheating to the point of shutting down.

You can look up in your Activity monitor to see who is consuming CPU.

Possible problems:

You cooling fan is not working at all or sporadic.

Your SMC (that controls the fan) needs to be rested.

Follow the instructions here based on your model to reset SMC.

You can also download the Intel Power app that will tell you what your CPU is doing.

The CPU is the single highest source of heat generation followed by GPU.

However, sometimes (rarely) the battery can do that due to the edit currents (internal) that will heat it up. Check you battery status and condition.