It’s hard to know for certain what truly motivates any political decision, but one thing that damages the argument that Britain outlawed the slave trade for purely altruistic reasons is that the country continued to benefit from slave labor well after it was technically illegal. Even after slave trading itself was abolished in 1807 (an effort spearheaded by William Wilberforce and others who, by all accounts, were genuinely dedicated to the project of abolition), slave labor itself was still employed in British colonies until 1833, and after that Britain continued to trade with and make use of goods produced by slaves.
And while voluntarily outlawing slavery may have been economically painful, there were other reasons to pursue the strategy aside from altruistic intent. The Haitian revolution had concluded in 1804 with the execution or expulsion of most of the white population and shocked slave-owning nations around the Atlantic (it also reduced demand by conclusively ending the trade in slaves to Saint-Domingue, which accounted for as many as 30 thousand slaves a year by the late 18th century).
Similarly, though Britain maintained colonies around the world, by the 1830s sugar (which relied especially heavily on slave labor) had long since stopped being profitable when compared to other British industries, which made abolishing slavery less of a bitter pill to swallow.
By the same token, the emergence of more “sophisticated” forms of forced labor like indenture in India and elsewhere made slavery much less necessary, and allowed Victorian Britons to maintain their sense of moral purity while continuing to benefit materially from unequal colonial and labor structures. When slavery was fully abolished in the British empire, it required a substantial pay-off to slave owners, and nothing to the slaves themselves.
/u/lordneobic has provided some compelling figures about how much abolishing slavery cost the empire, and it is true that there was a financial burden, but it stands to reason that if the British empire were driven by nothing more than pure humanitarian intent that abolition would have quicker and more comprehensive. As with so many aspects of history, the answer here is complicated. There were absolutely true believers and dedicated abolitionists who worked toward freedom for enslaved persons, but whether or not Parliament was acting with moral rectitude is much less clear.
EDIT: A comment that is now deleted asked me to clarify on some elements of this, so I'll add that here:
Sorry, it was imprecise of me to mention Parliament, and I probably should have been clearer -- I was also away from my research materials and unable to provide references and some details.
Can you speak with specificity about the parliamentary coalition that passed the legislation? The cleavages in the Tory and Whig parties at the time and how the leaders of various blocs in parliament were motivated to support/oppose abolition? How abolition furthered or undermined competing policy concerns such as the ongoing war with France?
I could, but with nowhere near as much confidence or clarity as someone with a background exclusively in British history could. You asked what my academic background is, and if you take a peek at my flair, it's "Atlantic History", which means a lot of things but in this context it means that I think the broader economic and social contexts are more important to this question that Parliamentary coalitions.
For example, Denmark passed an edict in 1792 abolishing the slave trade in their colonies -- the volume of trade to Danish holdings was fractional, insignificant compared to the British or Portuguese slave trade, but this still had the effect of putting pressure on British leaders, not exclusively because of moral reasons, but because of the social embarrassment of not being the first to do so (a point Wilberforce himself made). More on this later.
And though I mentioned this in my post above, I want to emphasize the role of the Haitian Revolution in all of this, an event that has largely been ignored in traditional historiography, but was hugely impactful around the Atlantic. Robin Blackburn makes the point in The Overthrow of Colonial Slavery that the triumphs of British abolitionism occurred at moments of exceptional national crisis and danger, and that the events of the 1790s and early 1800s "instilled a permanent panic in the minds of New World slave owners". Many of the arguments brought by abolitionists, like Lord Henry Brougham, were practical rather than moral -- that it was dangerous to continue to import large numbers of Africans to British colonies with the specter of revolution so nearby.
The assertion in the OP's question (and a celebrated aspect of British historiography) is that the British abolished slavery and the slave trade for "moral reasons and at great cost to themselves", which is, at best, misleading. Many British attitudes of abolitionism were driven by pity, not egalitarianism, and the desire to appear superior on the international stage. I do want to emphasize that while the 1807 and 1833 acts were hugely important to the decline of both the slave trade and slavery overall and should be credited as such, they were just as much public relations exercises in the eyes of many leading Britons, who hardly had generous feelings toward Africans or slaves. In many ways ending slavery was part of the same paternalistic "civilizing" impulse that led to British excesses all over their colonial holdings.
I get the sense that you might not find this answer satisfactory, and if you think my responses are insufficient to the criteria of this subreddit, that's fine, I won't be bothered if the mods agree, but I hope I've explained the reasoning behind my answer and done a better job of providing context for why I gave the answer that I did.
Sources:
Blackburn, Robin. The Overthrow of Colonial Slavery, 1776-1848
Blackburn, Robin. "Haiti, Slavery, and the Age of the Democratic Revolution." The William and Mary Quarterly, Third Series, 63, no. 4 (2006): 643-74.
Mason, Matthew. "Keeping up Appearances: The International Politics of Slave Trade Abolition in the Nineteenth-Century Atlantic World." The William and Mary Quarterly, Third Series, 66, no. 4 (2009): 809-32.
HUZZEY, RICHARD. "The Slave Trade and Victorian "Humanity"." Victorian Review 40, no. 1 (2014): 43-47.