How to limit the number of custom posts certain users can publish in WordPress using php script?

Welcome to StackExchange! I made some changes to your code that I pointed out with comments. I’m not sure if this will actually work so you have to try it, and probably your way of echoing the message won’t work because after updating there’s a redirect so you’ll have to work with the admin_notices action and find some way to persist the message after redirection, however I think that’s a topic for another question so I replaced the echoing part with an error_log which will be enough to know if this is working as intended by looking at the logs.

<?php
add_filter('wp_insert_post_data', 'tml_filter_by_membership', 99, 2);

/* 
When using a filter, the first argument is always the data to be modified and returned. 
Usually you'll do your checks to see if you want to modify something, otherwise you'll return
the first argument ($data) so other filters can work as well. If you return nothing,
then you will probably break something. Here you should use $data instead of $postarr,
which are the same array but the later ($postarr) is unescaped.
*/
function tml_filter_by_membership($data, $postarr)
{

    /* If you want to take only non-administrator users, don't check for capacity, check
 instead for that specific role. */
    if (!in_array('administrator', get_user_by('ID', $data['post_author'])->roles))
    {

        if ($data['post_type'] == 'gd_practitioner' || $data['post_type'] == 'gd_organization')
        {

            $membership_post_limits = tml_set_membership_post_limits_by_post_type();

            $author_memberships = tml_get_post_author_memberships($data['post_author']);

            $author_post_limit = tml_get_author_post_limit($author_memberships, $membership_post_limits, $data);

            $number_of_published_posts_by_author = tml_get_author_published_posts($data);
            /* I thought the last function was unnecessary so I just deleted it and moved
 it's code in here. */
            if ($number_of_published_posts_by_author >= $author_post_limit)
            {
                $data['post_status'] = 'draft';
                /* Check yours logs to see if this is actually working. Displaying the
 message is something you can solve afterwards. */
                error_log('You have exceeded the number of listings for your membership');
            }
            /* Return your modified object. Before this, you were using a function which
 returned something, but the return has to be inside the scope of the function linked to
 the filter in order to actually return it. Previously you were returning nothing. */
            return $data;
            
        }
    }

    return $data;
}

function tml_set_membership_post_limits_by_post_type()
{
    $membership_post_limits = Array(
        462 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        463 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        464 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        465 => Array(
            'gd_practitioner' => 10,
            'gd_organization' => 10
        ) ,
        466 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        467 => Array(
            'gd_practitioner' => 10,
            'gd_organization' => 10
        ) ,
        752 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        753 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        ) ,
        754 => Array(
            'gd_practitioner' => 1,
            'gd_organization' => 1
        )
    );
    return $membership_post_limits;
}

function tml_get_post_author_memberships($user_id)
{
    if (class_exists('MeprUser'))
    {

        $user = new MeprUser($user_id);
        $get_memberships = $user->active_product_subscriptions();
        $author_memberships = array_values(array_unique($get_memberships));
        return $author_memberships;
    }
    else
    {
        return false;
    }
}


function tml_get_author_post_limit($author_memberships, $membership_post_limits, $data)
{
    $author_post_limit = 1;
    foreach ($author_memberships as $membership)
    {
        if ($author_post_limit < $membership_post_limits[$membership][$data['post_type']])
        {
            $author_post_limit = $membership_post_limits[$membership][$data['post_type']];
        }
    }
    return $author_post_limit;
}

function tml_get_author_published_posts($data)
{

    return count(get_posts(array(
        'author' => $data['post_author'],
        'post_type ' => $data['post_type'],
        'numberposts ' => - 1,
        'post_status ' => 'publish'
    )));

}