it is because, at any given time, there are hundreds, to thousands, of family, and friends, of patients in the hospital. Hospitals are large places, hard to navigate when you don't know them, and even then take a long time to get in and out of. Also they have a similar amount of various employees. People got tired of the cafeteria food, and began voicing complaints about how big of a pain in the ass getting food that doesn't suck in. Also, a lot, even most, of the people in the hospital are not there due to dietary stemming problems. When you are sick and/or injured, you want comfort foods. People want to bring you comfort food.
So, over time, they started making space for commercial food service to rent from them. So you will see fast food, and sometimes whole on chain restaurants. This also can be a source of income.
honestly i really kinda like big hospitals for this reason, they just end up feeling like an entire ultradense town, i have to imagine something like this is what it would feel like to be onboard a big star trek spaceship.