A good example might be a large 0% credit card balance of £9000.
Would you invest the £9000 and pay the monthly £220 or clear the debt?
Well like you mentioned it comes down to risk and whether your gains from the investment will (or likely will) exceed the interest on the debt... in the case of 0% interest on debt it is a bit of a no brainer for any investment with a fixed or guaranteed return (at least assuming it is accessible by the time the 0% period is over), unless you're so short of cash you can't afford any mandatory payments.
On the other hand, a tracker (or stock market investments in general) perhaps aren't a good idea to be relied on if you need access to the money in the short term, the expectation might be that the tracker will perform well in the long run but in any given year it might be down and if you were reliant on that investment to clear your credit card debt then you might not wish to take that risk, alternatively, if you have other funds available then you perhaps don't care and again 0% interest is a nice freeby so no reason to pay it off when your money is better utilised elsewhere...
Doesn’t it depend on the level of mortgage vs level of investment? If your mortgage is 300k but investment only 50k, is there an argument to reduce the mortgage or do you ignore the cost of repaying the capital when making that decision?
If you can get a better rate then yeah it could be better to overpay, otherwise no, if you're earning more with your investments then overpaying your (lower rate) mortgage is going to cost you.
Super simplified example where we ignore taxes, remortgaging costs and pretend interest applies annually:
Assume you pay 3.5% on your 300k mortgage and earn 5% from your 50k savings (we got in a time machine back to the 00s), well that's -£10,500 + £2,500 = -£8000
whereas if you paid it off and had a £250k mortgage at 3.5% then that would be -£8,750
however, if the mortgage rate improved by paying off the 50k that could change things, supposing paying it off means you only pay 3%, well your £250k mortgage now only costs -£7,500 so better than keeping it at 300k at a higher interest rate + the 50k savings.
(obviously, that is super simplified for the reasons mentioned but also other things might come into play - for example, if there is a significant change perhaps that you might want to use that money in future. Some mortgages are flexible with overpayments and you can get the cash back out, others aren't and you might need to then arrange to drawdown on the mortgage, give justifications for why you want the money etc.. and potentially face a higher interest charge on that amount... all of which would be avoided if you simply had it sat in your savings account instead.)