Creating a custom feed for categories that includes the first post’s featured image

First define a function to return the first post of a category by a given category ID ex:

function get_category_post($cat_id){
    $post_args = array(
        'numberposts' => 1,
        'category' => $cat_id,
        'fields' => 'ids'
    );
    $posts = get_posts($post_args);
    return $posts[0];
}

Then, once you have that function you can use it like this:

function outputXMLFeed()
{
    echo '<?xml version="1.0"?>';
    echo '<items>';
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $categories=get_categories($args);
    foreach($categories as $category) { 
        echo '<item>';
        echo '<catID>' . $category->term_id . '</catID>';
        echo '<catname>' . $category->name . '</catname>';
        echo '<postcount>' . $category->category_count . '</postcount>';
        echo '<slug>' . $category->slug . '</slug>';
        echo '<featured>' . get_the_post_thumbnail(get_category_post($category->term_id) , $size="post-thumbnail") . '</featured>';
        echo '</item>';
    }
    echo '</items>';
}
add_action('init', 'add_my_feed');

function add_my_feed(  ) {
  add_feed("myFeed", "outputXMLFeed");
}