I ended up removing that code from my functions.php file and used jQuery instead. Here’s how I took care of it, if anyone wants to know:
$('.menu-item-has-children > a').each(function(){
var submenuHeading = $(this).text();
$('.sub-menu', ($(this).parent('.menu-item-has-children'))).prepend('<h3>' + submenuHeading + '</h3>');
});