Typically loan packagers use some law of large numbers type math to at least make the marketing attractive enough. Obviously this can blow up (see: 2008 GFC: MBS, CDOs, CDO^2, etc).
The attraction to investors is something like - doctors are paying 7% interest on their loans, government 10~30yr treasuries are yielding 4.5-5%. Individual doctors have a much higher risk of defaulting than the US government. If you pull 1000s of them together, some will still default but you need like 30%+ of them to default for the increased yield to not reward you for the risk. For certain types of investors (pensions, insurance companies, etc) having something that pays a little more than "risk free rate" without taking on too much risk is a big benefit.
Someone who knows fixed income more than me may chime in with lots of other nuances like early repayment risk, forbearance, re-financing, loan modifications, etc but this is the general framework.
And the guys structuring these things make money off commissions on sale, making a market between buyers&sellers of these products (the spread) and probably some some sort of management fees.