How do I fix Undefined variable using $_POST in function?

Your check is incorrect: right now, it tries to access $_POST['rating'], which might not be set, and then checks whether that value is empty. Furthermore, you should scope your if statement with brackets, because right now, your code will try and update the average rating even if no new rating is provided. Try:

function save_comment_meta_rating( $comment_id ){
    if ( ! array_key_exists( 'rating', $_POST ) || empty( $_POST['rating'] ) ) {
        return;
    }

    $rating = sanitize_text_field( $_POST['rating'] );
    add_comment_meta( $comment_id, 'rating', $rating );

    // Then update the average rating.
    update_post_avg_rating( $comment_id );  
}
add_action( 'comment_post', 'save_comment_meta_rating' );