Agreed, but I would go further and say that if demand by students for the training provided by residency exceeds the demand by hospitals for the work provided by residents, I don't see why residents couldn't pay for their training just as they do for medical school. The whole "residency funding" thing seems like a red herring as an explanation.
To be clear, I'm not saying that medical graduates should have to take on more debt to pay for residency, but rather that the reason this doesn't happen is not obvious according to typical economic reasoning.