problems with comments_number()

comments_number() echo’s the number of comments, while get_comments_number() returns the number of comments as a string, so what you are likely looking for is get_comments_number instead of comments_number, like

echo '<ul>';
while ( $query->have_posts() ) {
    $query->the_post();

    $title = get_the_title();
    echo '<li>' . $title . get_comments_number() . '</li>';
}
echo '</ul>';

https://codex.wordpress.org/Template_Tags/get_comments_number

error code: 523