Inside this loop:
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class="gallery-item">";
$output .= "<span class="photos-comment-number-wrap"><span class="comment-number">". comments_number() ."</span></span>";
$output .= "
<{$icontag} class="gallery-icon">
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class="gallery-caption">
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
You can get at the comment count directly from the object:
$attachment->comment_count
Refer to the wp_posts table in the Database Description Codex entry for more information.