Hi Aayush740,
Welcome to the Apple Community.
Thank you for your question, and also for providing good info. on the situation you are experiencing.
Wondering which MacBook Pro model (year) you are using,
Re: "restarting on its own again and again at random times"
If you have any third-party (non-Apple) security, anti-virus or maintenance apps, or may have incompatible apps installed,
please see the info below from this discussion thread: Antivirus Software for Catalina - Apple Community
eg: says:
"The best antivirus, cleaning app, and overall maintenance app for Mac OS is Mac OS itself.
All you need to do with Mac OS to keep it secure and running well is to keep it up-to-date and do periodic (about 1x per week) restarts Other than that, leave it alone.
Adding third party antivirus, cleaning, security and other types of maintenance apps to Mac OS adds no additional level of security. The only thing these apps do is have the opposite affect users want. They make Mac OS slow, unstable, generate odd behavior and make Mac OS appear buggy."
Solution:
Turn off any such apps, experiment to find the culprit, and / or obtain uninstaller(s) from the app developer(s).
If installed: check any necessary third-party apps, (eg VPN) are updated to remain compatible with your Mac OS.
__________________________________________________________________________________________
If third-party app(s) are no problem:
With the article below: You might want to work through any trouble-shooting steps that you have not yet tried:
If your Mac restarted because of a problem - Apple Support
And:
"If you continue to experience unexpected restarts, contact Apple Support.
If your Mac is frequently restarting unexpectedly, it's important to determine the exact steps that lead up to the issue.
✽ The next time that your Mac restarts unexpectedly, record the date and time it occurs.
These questions might help you diagnose the problem:
✽ Was the computer starting up, shutting down, or performing a particular task when the unexpected restart happened?
✽ Is the computer restart random, or does it happen every time you do a certain task?
✽ Does the restart happen when a specific external device is connected to your Mac or to a specific port?"
_________________________________________________________________________________________________
To try checking for hardware issues: If your Mac restarts and a message appears – Apple Support
_________________________________________________________________________________________________
Re: "touch bar disappears sometimes"
Discussion thread with suggestions:
Macbook keeps restarting and Touch Bar no… - Apple Community
All the best :-)