>>17995909
It's because Britain had no idea what to do with India and was honestly uncomfortable and self-contractory with the rationale it held it in the first place.
Britain conquered India, plain and simple. That's fine, India have been conquered countless times and conquest itself was always seen as a reason good enough to hold onto some territory. After all "right of conquest" was an actual legal institution back in the day. But the problem is that from the very start Britain was self-contradictory and hypocritical about it. They claimed it wasn't conquest, but something done for the benefit of Indians themselves. Sort of early manifestation of the SF trope of aliens coming in and uplifting the natives.
So Britain at the same time exploited India like the right of conquest entitled them to do so. But at the same time they did some gestures towards improving the lot of Indians like producing some reforms or granting a narrow group of Indian elites Western education. But this contradiction created resentment. The newly educated elites were painfully conscious that they aren't British and that they are subjugated people. And so they started their own movement for independence. Had Britain kept India in the dark, none of that would happen. Had Britain simply hanged the nationalists like all conquerors do, they would also have no problem with keeping India. But because of the veneer of benevolent uplifters, they were unable to do so.
Finally, you had ww1 and ww2 which caused the financial ruin of Britain. It also gave the British population an insane amount of war wariness. British population had no stomach to fight to keep India in. So when it turned out that the Indian Nationalists successfully ingrained themselves in the Indian Army, the British just let India go rather than risking another bloody war to keep the jewel of their empire.