This is an issue on some Google Pixel XL phones. Google released a software update in February of 2017 that fixes this issue. If you have not already, update your phone to the newest version. If this doesn't work, you should avoid the problem by not using max volume or avoiding apps that cause the distortion. Then, report the problem to Google.
Try taking off your phone case. Some cases insulate the phone in a way that makes the phone overheat. If this doesn't work, try closing apps that are not in use. Also, do not use very intensive apps for large amounts of time. Finally, if all else fails you might want to replace your battery. There is a guide to learn how to replace your battery on the device page here on iFixit.